package com.ebao.action.base;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;

/*
 * 提供了2种获取request、session的方法
 */
public class BaseAction extends ActionSupport implements RequestAware, SessionAware {

	private static final long serialVersionUID = 3637135435910790708L;
	
	protected transient final Logger logger = LoggerFactory.getLogger(getClass());

	protected Map<String, Object> request;
	protected Map<String, Object> session;

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}
	
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	/**
	 * 获取request
	 * 
	 * @return
	 */
	protected HttpServletRequest getServletRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 获取session
	 * 
	 * @return
	 */
	protected HttpSession getSession() {
		return ServletActionContext.getRequest().getSession();
	}

	/**
	 * 获取response
	 * 
	 * @return
	 */
	protected HttpServletResponse getServletResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * 获取response的输出类
	 * 
	 * @return
	 * @throws Exception
	 */
	private PrintWriter getResponseWriter() throws Exception {
		HttpServletResponse response = getServletResponse();
		response.setContentType("text/xml;charset=UTF-8");
		return response.getWriter();
	}

	/**
	 * 将JSONObject对象转换为字符串，返回给客户端
	 * 
	 * @param jo 保存数据的JSONObject对象
	 * 
	 * @throws Exception
	 */
	protected void writeJSON(JSONObject jo) throws Exception {
		PrintWriter out = getResponseWriter();
		out.write(jo.toString());
		close(out);
	}

	/**
	 * 将JSONObject对象转换为字符串，返回给客户端
	 * 
	 * @param jsonStr JSON格式的字符串。建议使用标准json字符串，如{"name":"liuwei","age":"23"}，即都用双引号。
	 * 
	 * @throws Exception
	 */
	protected void writeJSON(String jsonStr) throws Exception {
		PrintWriter out = getResponseWriter();
		out.write(jsonStr);
		close(out);
	}

	/**
	 * 将JSONObject对象转换为字符串，返回给客户端
	 * 
	 * @param jo 保存数据的JSONArray对象
	 *            
	 * @throws Exception
	 */
	protected void writeJSON(JSONArray jo) throws Exception {
		PrintWriter out = getResponseWriter();
		out.write(jo.toString());
//		out.flush();
		close(out);
	}
	
	/**
	 * 结束输出流
	 * 
	 * @param out
	 * @throws Exception
	 */
	private void close(PrintWriter out) throws Exception {
		if (out != null) {
			out.close();
		}
	}
	
	/**
	 * 向客户端回写内容
	 * 
	 * @param msg
	 */
	protected void writeResponseMsg(String msg) {
		try {
			getServletResponse().setContentType("text/plain");
			getServletResponse().getWriter().print(msg);
			getServletResponse().flushBuffer();
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
	}

	protected void writeResponseMsgAsHtml(String msg) {
		try {
			getServletResponse().setContentType("text/html");
			getServletResponse().getWriter().print(msg);
			getServletResponse().flushBuffer();
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
	}


}
